package com.tzinfo.tzt.activity;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.tzinfo.tzt.R;
import com.tzinfo.tzt.util.HttpUtils;
import com.tzinfo.tzt.util.LifeDetailResult;
import com.tzinfo.tzt.util.ResultMessagee;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2014/11/19.
 */
public class CarLifeDetailActivity extends Activity{
    private ImageView iv_vatar;
    private ImageView iv_attachment;
    private TextView tv_uid;
    private TextView tv_name;
    private TextView tv_subject;
    private TextView tv_dateline;
    private TextView tv_context;
    private TextView tv_regdate;
    private int id;
    private ImageLoader imageLoader;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_carlife);

        Bundle extras = getIntent().getExtras();
        id = extras.getInt("tid");

        iv_vatar =(ImageView)findViewById(R.id.list_item_image);
        tv_uid = (TextView)findViewById(R.id.list_item_netname);
        tv_name = (TextView)findViewById(R.id.list_item_credits);
        tv_subject= (TextView)findViewById(R.id.list_item_title);
        tv_dateline = (TextView)findViewById(R.id.list_item_time);
        iv_attachment =(ImageView)findViewById(R.id.img_attachment);
        tv_context = (TextView)findViewById(R.id.list_item_context);
        tv_regdate = (TextView)findViewById(R.id.list_item_regdate);

        imageLoader = ImageLoader.getInstance();
        new GetData().execute();


    }



    public class GetData extends AsyncTask<String,Void,LifeDetailResult> {

        @Override
        protected LifeDetailResult doInBackground(String... params) {
            LifeDetailResult result;
            List<NameValuePair> pairs = new ArrayList<NameValuePair>();
            pairs.add(new BasicNameValuePair("tid", String.valueOf(id)));

            String GetDataUrl = "http://tzt.tzinfo.net/mob/api/ForumpostList.ashx";
            String loginResultString = HttpUtils.doPostMethod(GetDataUrl, pairs);

            Gson gson = new Gson();
            result = gson.fromJson(loginResultString, LifeDetailResult.class);
            return result;
        }

        @Override
        protected void onPostExecute(LifeDetailResult result) {
            super.onPostExecute(result);
            if (result.getData().size()>0) {
                tv_subject.setText(result.getData().get(0).getSubject());
                tv_name.setText(result.getData().get(0).getUserinfo().getUsername());
                tv_uid.setText(String.valueOf(result.getData().get(0).getUserinfo().getUid()));

                tv_dateline.setText(result.getData().get(0).getDateline());
                tv_regdate.setText(result.getData().get(0).getUserinfo().getRegdate());

                tv_context.setText(result.getData().get(0).getMessage());


                imageLoader.displayImage(result.getData().get(0).getUserinfo().getAvatar(), iv_vatar);
                if (result.getData().get(0).getAttachment().size() > 0)
                    imageLoader.displayImage(result.getData().get(0).getAttachment().get(0).getAttachment().toString(), iv_attachment);
            }
            else {
                Toast.makeText(CarLifeDetailActivity.this,"没有数据",Toast.LENGTH_SHORT).show();
            }
        }
    }
    public void goBack(View v) {
        this.finish(); //关闭当前Activity
    }
}
